package com.plian.system.service.pf.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.pf.MainAnnual;
import com.plian.system.entity.pf.MainBusiness;
import com.plian.system.mapper.pf.MainAnnualMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.pf.IMainAnnualService;
import com.plian.system.service.pf.IMainBusinessService;
import com.plian.system.vo.pf.MainAnnualVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO
 * @Date: 2023/1/3 8:26 PM
 * @Version: 1.0
 **/

@Slf4j
@Service
@AllArgsConstructor
public class MainAnnualServiceImpl extends BaseServiceImpl<MainAnnualMapper, MainAnnual> implements IMainAnnualService {

    private final IMainBusinessService mainBusinessService;

    @Override
    public boolean deleteByParentId(List<Long> ids) {
        LambdaQueryWrapper<MainAnnual> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(MainAnnual::getBaseBusinessAnnualId, ids);
        List<MainAnnual> mainAnnuals = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(mainAnnuals)){
            return deleteLogic(mainAnnuals.stream().map(MainAnnual::getId).collect(Collectors.toList()));
        }
        return true;
    }


    @Override
    public void setDetailList(List<MainAnnualVO> mainAnnualVOS){
        List<MainBusiness> list = mainBusinessService.list(new LambdaQueryWrapper<MainBusiness>()
                .in(MainBusiness::getId, mainAnnualVOS.stream().map(MainAnnualVO::getMainBusinessId).collect(Collectors.toList())));
        if (CollectionUtil.isNotEmpty(list)){
            //放入
            mainAnnualVOS.forEach(m -> {
                List<MainBusiness> mainBusinesses = list.stream().filter(index -> m.getMainBusinessId().equals(index.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(mainBusinesses)){
                    MainBusiness vo = mainBusinesses.get(0);
                    m.setMainBusinessId(vo.getId());
                    m.setIndexNum(vo.getIndexNum());
                    m.setEvaluation(vo.getEvaluation());
                }
            });
        }
    }

}
